#!/usr/bin/env python

#############################################################################
# ParaTrac: Scalable Tracking Tools for Parallel Applications
# Copyright (C) 2009,2010  Nan Dun <dunnan@yl.is.s.u-tokyo.ac.jp>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

#
# fstrac
# Filesystem Trace
#

import os
import sys

from modules.verbose import Progress

def import_data(path):
    from fs.data import Database
    db = Database("%s/trace.sqlite" % path)
    pgs = Progress("Importing logs from %s ..." % path, " Done!\n")
    pgs.start()
    try:
        db.import_logs()
    except:
        pgs.cancel()
        raise
    pgs.end()

def generate_report(path):
    dbpath = "%s/trace.sqlite" % path
    if not os.path.exists(dbpath): import_data(path)
    pgs = Progress("Generating report to %s ..." % path, " Done!\n")
    pgs.start()
    try:
        from fs.report import HTMLReport
        r = HTMLReport(dbpath)
        r.write()
    except:
        pgs.cancel()
        raise

    pgs.end()

def plotting(path, plist):
    from fs.plot import Plot
    dbpath = "%s/trace.sqlite" % path
    if not os.path.exists(dbpath): import_data(path)
    pgs = Progress()
    pgs.start("Plotting from %s ..." % path)
    try:
        p = Plot(path)
        p.plot(plist)
    except:
        pgs.cancel()
        raise

    pgs.end("\nDone!\n")

def main(argv):
    from fs.opts import Options
    opt = Options(argv)
    
    if opt.opts.import_dir:
        import_data(opt.opts.import_dir)
    
#    if opt.opts.plot:
#        plotting(opt.opts.path, opt.opts.plot)

    if opt.opts.report_dir:
        generate_report(opt.opts.report_dir)

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv))

#EOF
